package com.lzm.ydpt.module.riding.fragment;


import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.byt.framlib.commonwidget.selectordialog.dialog.DialogOnClickListener;
import com.byt.framlib.commonwidget.selectordialog.dialog.NormalAlertDialog;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.riding.OrderBean;
import com.lzm.ydpt.mvp.contract.HomeContract;
import com.lzm.ydpt.mvp.presenterimpl.HomePresenterImpl;
import com.lzm.ydpt.view.pop.UserStateCommonPop;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页骑行
 */
public class RidingHomeFragment extends BaseFragment<HomePresenterImpl> implements HomeContract.View, UserStateCommonPop.OnItemOnClickListener {

    private static RidingHomeFragment sFragment;

    @BindView(R.id.rl_riding_home_title)
    RelativeLayout rl_riding_home_title;
    @BindView(R.id.ll_show_riding_state)
    LinearLayout ll_show_riding_state;
    @BindView(R.id.tv_riding_home_state)
    TextView tv_riding_home_state;
    @BindView(R.id.img_show_state)
    ImageView img_show_state;
    @BindView(R.id.image_right_refrsh)
    ImageView image_right_refrsh;
    @BindView(R.id.srl_riding_home)
    SmartRefreshLayout srl_riding_home;
    @BindView(R.id.rv_riding_home)
    RecyclerView rv_riding_home;
    @BindView(R.id.ltp_home_order_data)
    LoadingTip ltp_home_order_data;
    private RvCommonAdapter<OrderBean> adapter = null;
    private List<OrderBean> mList = new ArrayList<>();
    private UserStateCommonPop mViewBottomPop = null;

    public static RidingHomeFragment newsInstance() {
        sFragment = new RidingHomeFragment();
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_riding_home;
    }

    @Override
    public HomePresenterImpl initPresenter() {
        return new HomePresenterImpl(this);
    }

    @Override
    protected void initView() {
        initRv();
        initAdapter();
    }

    private void initRv() {
        setRefreshLayout(srl_riding_home);
        srl_riding_home.setEnableLoadMore(false);
        srl_riding_home.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffWhite());
        srl_riding_home.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                fetchData();
            }
        });
    }


    private void fetchData() {
    }

    /**
     * 接单状态 1接单中 0休息中
     *
     * @param receiveStatus
     */
    private void edtUserSate(int receiveStatus) {
        startProgressDialog();
        FormBodys formBodys = new FormBodys.Builder()
                .add("memberId", Dapplication.getUserToken())
                .add("receiveStatus", receiveStatus)
                .build();
        mPresenter.onEdtState(formBodys, receiveStatus);
    }

    private void initAdapter() {
        mList.add(new OrderBean());
        mList.add(new OrderBean());
        mList.add(new OrderBean());
        mList.add(new OrderBean());
        rv_riding_home.setLayoutManager(new LinearLayoutManager(context));
        adapter = new RvCommonAdapter<OrderBean>(context, mList, R.layout.item_order_home_rv) {
            @Override
            protected void convert(RvViewHolder holder, OrderBean orderBean, int position) {
//                 holder.setText(R.id.tv_count_price,);
//                 holder.setText(R.id.tv_add_price,);
//                 holder.setText(R.id.tv_order_deliver_time,);
//                 holder.setText(R.id.tv_send_order_addrss,);
//                 holder.setText(R.id.tv_send_order_distance,);
//                 holder.setText(R.id.tv_collect_order_addrss,);
//                 holder.setText(R.id.tv_collect_order_distance,);
//                 holder.setText(R.id.tv_order_todo,);
                TextView tv_submit_order_data = holder.getView(R.id.tv_submit_order_data);
                if (Dapplication.getUserInfo() != null && Dapplication.getUserInfo().getReceiveStatus() == 1) {
                    tv_submit_order_data.setEnabled(true);
                } else {
                    tv_submit_order_data.setEnabled(false);
                }
                holder.setOnClickListener(R.id.tv_submit_order_data, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        showCollectDialog(orderBean);
                    }
                });
            }
        };
        rv_riding_home.setAdapter(adapter);
    }

    private void showCollectDialog(OrderBean orderBean) {
        NormalAlertDialog normalAlertDialog = new NormalAlertDialog.Builder(context)
                .setButtonTextSize(14)
                .setTitleVisible(true)
                .setTitleTextColor(R.color.color_333333)
                .setTitleText("确认接单")
                .setTitleTextSize(17)
                .setContentText("你准备好接单了吗？")
                .setContentTextSize(17)
                .setContentTextColor(R.color.color_666666)
                .setRightButtonTextColor(R.color.color_main)
                .setLeftButtonTextColor(R.color.color_bbbbbb)
                .setOnclickListener(new DialogOnClickListener() {
                    @Override
                    public void clickLeftButton(View view) {
                    }

                    @Override
                    public void clickRightButton(View view) {

                    }
                }).build();
        normalAlertDialog.show();
    }


    @Override
    public void initFristData() {
        super.initFristData();
//        ltp_home_order_data.setLoadingTip(LoadingTip.LoadStatus.loading);
//        fetchData();
    }

    @OnClick({R.id.ll_show_riding_state, R.id.image_right_refrsh})
    public void onClick(View view) {
        if (AntiShake.check(view.getId())) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_show_riding_state:
                if (mViewBottomPop != null && mViewBottomPop.isShowing()) {
                    mViewBottomPop.dismiss();
                    return;
                }
                if (mViewBottomPop == null) {
                    mViewBottomPop = new UserStateCommonPop(context, (int) context.getResources().getDimension(R.dimen.x210),
                            (int) context.getResources().getDimension(R.dimen.x230), this);
                    mViewBottomPop.setHoneDetailsUi();
                }
                mViewBottomPop.showAsDropDown(ll_show_riding_state, img_show_state);
                break;
            case R.id.image_right_refrsh:
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        sFragment = null;
    }


    @Override
    public void showMessage(String message, String method) {
        srl_riding_home.finishRefresh();
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {

    }


    @Override
    public void onItemClick(String item, int index) {
        switch (index) {
            case 0:// 接单
                if (Dapplication.getUserInfo() != null && Dapplication.getUserInfo().getReceiveStatus() == 1) {
                    // 接单
                    return;
                }
                edtUserSate(1);
                break;
            case 1:// 休息
                if (Dapplication.getUserInfo() != null && Dapplication.getUserInfo().getReceiveStatus() == 0) {
                    // 接单
                    return;
                }
                edtUserSate(0);
                break;
        }
    }

    @Override
    public void onOrderList(List<OrderBean> orderBeans) {
        mList.clear();
        mList.addAll(orderBeans);
        adapter.notifyDataSetChanged();
        if (mList.size() == 0) {
            ltp_home_order_data.setLoadingTip(LoadingTip.LoadStatus.empty);
            ltp_home_order_data.setTipLogo(R.drawable.empty_order_lilst);
        } else {
            ltp_home_order_data.setLoadingTip(LoadingTip.LoadStatus.finish);
        }
    }

    @Override
    public void onEdtState(String message, int receiveStatus) {
        stopProgressDialog();
        showShortToast(message);
        adapter.notifyDataSetChanged();
        if (Dapplication.getUserInfo() != null) {
            Dapplication.getUserInfo().setReceiveStatus(receiveStatus);
        }
        tv_riding_home_state.setText(receiveStatus == 1 ? "接单中" : "休息中");
    }
}
